package com.chaosyeb.server.controller;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.chaosyeb.server.pojo.Joblevel;
import com.chaosyeb.server.pojo.RespBean;
import com.chaosyeb.server.service.IJoblevelService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 职称
 *
 * @author Chaos
 * @since 2021-12-04
 */
@RestController
@RequestMapping("/jobLevel")
public class JobLevelController {
    @Autowired
    private IJoblevelService joblevelService;

    @ApiOperation(value = "添加职称")
    @PostMapping("/add")
    public RespBean add(@RequestBody Joblevel joblevel){
        joblevel.setCreateDate(LocalDateTime.now());
        if (joblevelService.save(joblevel)) {
            return RespBean.success("添加职位成功");
        }
        return RespBean.error("添加失败");
    }

    @ApiOperation(value = "删除职位")
    @DeleteMapping("/delete/{id}")
    public RespBean delete(@PathVariable Integer id){
        if (joblevelService.removeById(id)) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/deleteAll/{ids}")
    public RespBean deleteByIds(@PathVariable Integer[] ids){
        if (joblevelService.removeByIds(Arrays.asList(ids))) {
            return RespBean.success("批量删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "更新职位")
    @PutMapping("/update")
    public RespBean update(@RequestBody Joblevel joblevel){
        if (joblevelService.updateById(joblevel)) {
            return RespBean.success("更新职位成功");
        }
        return RespBean.error("更新失败");
    }

    @ApiOperation(value = "查找职位")
    @GetMapping("/find")
    public RespBean find(){
        List<Joblevel> list = joblevelService.list();
        if (CollectionUtils.isEmpty(list)){
            return RespBean.error("查询失败");
        }
        return RespBean.success("查询成功", list);
    }
}
